home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / matrx042.zip / MATDET.C < prev    next >
C/C++ Source or Header  |  1994-04-16  |  2KB  |  116 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    matdet.c
  4. *    desc:    determinant calculations
  5. *    by:    ko shu pui, patrick
  6. *    date:    21 may 92 v0.3
  7. *    revi:
  8. *    ref:
  9. *       [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene,"
  10. *    John Wiley & Sons, 2nd Ed., 1983. Chap 3.
  11. *
  12. *-----------------------------------------------------------------------------
  13. */
  14. #include <stdio.h>
  15. #include "matrix.h"
  16.  
  17. static double signa[2] = {1.0, -1.0};
  18.  
  19. /*
  20. *-----------------------------------------------------------------------------
  21. *    funct:    mat_minor
  22. *    desct:    find minor
  23. *    given:    A = a square matrix,
  24. *        i=row, j=col
  25. *    retrn:    the minor of Aij
  26. *-----------------------------------------------------------------------------
  27. */
  28. double mat_minor( A, i, j )
  29. MATRIX A;
  30. int i, j;
  31. {
  32.     MATRIX    S;
  33.     double    result;
  34.  
  35.     S = mat_submat(A, i, j);
  36.     result = mat_det( S );
  37.     mat_free(S);
  38.  
  39.     return (result);
  40.  
  41. }
  42.  
  43. /*
  44. *-----------------------------------------------------------------------------
  45. *    funct:    mat_cofact
  46. *    desct:    find cofactor
  47. *    given:    A = a square matrix,
  48. *        i=row, j=col
  49. *    retrn:    the cofactor of Aij
  50. *-----------------------------------------------------------------------------
  51. */
  52. double mat_cofact( A, i, j )
  53. MATRIX A;
  54. int i, j;
  55. {
  56.     double    result;
  57.  
  58.     result = signa[(i+j)%2] * A[i][j] * mat_minor(A, i, j);
  59.  
  60.     return (result);
  61. }
  62.  
  63. /*
  64. *-----------------------------------------------------------------------------
  65. *    funct:    mat_det
  66. *    desct:    find determinant
  67. *    given:    A = matrix
  68. *    retrn:    the determinant of A
  69. *    comen:
  70. *-----------------------------------------------------------------------------
  71. */
  72. double mat_det( a )
  73. MATRIX a;
  74. {
  75.     MATRIX    A, P;
  76.     int    i, j, n;
  77.     double    result;
  78.  
  79.     n = MatRow(a);
  80.     A = mat_copy(a);
  81.     P = mat_creat(n, 1, UNDEFINED);
  82.  
  83.     /*
  84.     * take a LUP-decomposition
  85.     */
  86.     i = mat_lu(A, P);
  87.     switch (i)
  88.         {
  89.         /*
  90.         * case for singular matrix
  91.         */
  92.         case -1:
  93.         result = 0.0;
  94.         break;
  95.  
  96.         /*
  97.         * normal case: |A| = |L||U||P|
  98.         * |L| = 1,
  99.         * |U| = multiplication of the diagonal
  100.         * |P| = +-1
  101.         */
  102.         default:
  103.         result = 1.0;
  104.         for (j=0; j<MatRow(A); j++)
  105.             {
  106.             result *= A[(int)P[j][0]][j];
  107.             }
  108.         result *= signa[i%2];
  109.         break;
  110.         }
  111.  
  112.     mat_free(A);
  113.     mat_free(P);
  114.     return (result);
  115. }
  116.